package com.lzy.cloud.permission.controller;

import com.lzy.cloud.permission.dtos.PermissionDto;
import com.lzy.cloud.permission.dtos.ResourceDto;
import com.lzy.cloud.permission.dtos.RoleResourcePermissionDto;
import com.lzy.cloud.permission.request.RoleResourcePermissionRequest;
import com.lzy.cloud.permission.response.RoleResourcePermissionResponse;
import com.lzy.cloud.permission.service.PermissionService;
import com.lzy.cloud.permission.service.ResourceService;
import com.lzy.cloud.permission.service.RoleResourcePermissionService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WIN7 on 2018/5/17.
 */
@RestController
@RequestMapping("/sys/role/resource/permission")
public class RoleResourcePermissionController {

    @Autowired
    RoleResourcePermissionService roleResourcePermissionService;

    @Autowired
    ResourceService resourceService;

    @Autowired
    PermissionService permissionService;

    /**
     * 新增角色资源权限关联
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add")
    public Result add(@RequestBody(required = false) RoleResourcePermissionRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = roleResourcePermissionService.insert(DozerUtils.map(request, RoleResourcePermissionDto.class));
        Ensure.that(ok).isTrue("00050001");
        return new Result();
    }

    /**
     * 删除角色资源权限关联
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/del")
    public Result del(@RequestBody RoleResourcePermissionRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = roleResourcePermissionService.delete(DozerUtils.map(request, RoleResourcePermissionDto.class));
        Ensure.that(ok).isTrue("00050002");
        return new Result();
    }

    /**
     * 根据条件查询列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    public Result selectList(@RequestBody RoleResourcePermissionRequest request) {
        Ensure.that(request).isNotNull("00010001");
        List<RoleResourcePermissionDto> roleResourcePermissionDtoList = roleResourcePermissionService.selectList(DozerUtils.map(request, RoleResourcePermissionDto.class));
        if (roleResourcePermissionDtoList != null && roleResourcePermissionDtoList.size() > 0) {
            return new Result(DozerUtils.maps(roleResourcePermissionDtoList, RoleResourcePermissionResponse.class));
        }
        return new Result();
    }

    /**
     * 根据角色id查找对应的资源列表
     *
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/select/resource/list/{roleId}", method = RequestMethod.GET)
    public Result selectResourceListByRole(@PathVariable("roleId") Long roleId) {
        RoleResourcePermissionDto roleResourcePermissionDto = new RoleResourcePermissionDto();
        roleResourcePermissionDto.setRoleId(roleId);
        List<RoleResourcePermissionDto> roleResourcePermissionDtoList = roleResourcePermissionService.selectList(roleResourcePermissionDto);
        List<ResourceDto> resourceDtoList = new ArrayList<>();
        for (RoleResourcePermissionDto resourcePermissionDto : roleResourcePermissionDtoList) {
            ResourceDto resourceDto = resourceService.selectByPrimaryKey(resourcePermissionDto.getResourceId());
            if (!resourceDtoList.contains(resourceDto)) {
                resourceDtoList.add(resourceDto);
            }
        }
        return new Result(resourceDtoList);
    }

    /**
     * 根据资源id查找对应的权限
     *
     * @param resourceId
     * @return
     */
    @RequestMapping(value = "/select/permission/{resourceId}", method = RequestMethod.GET)
    public Result selectPermissionByRes(@PathVariable("resourceId") Long resourceId) {
        RoleResourcePermissionDto roleResourcePermissionDto = new RoleResourcePermissionDto();
        roleResourcePermissionDto.setResourceId(resourceId);
        List<RoleResourcePermissionDto> roleResourcePermissionDtoList = roleResourcePermissionService.selectList(roleResourcePermissionDto);
        List<PermissionDto> permissionDtoList = new ArrayList<>();
        for (RoleResourcePermissionDto resourcePermissionDto : roleResourcePermissionDtoList) {
            PermissionDto permissionDto = permissionService.selectById(resourcePermissionDto.getPermissionId());
            if (!permissionDtoList.contains(permissionDto)) {
                permissionDtoList.add(permissionDto);
            }
        }
        return new Result(permissionDtoList);
    }


}
